//
//  LLMainViewModel.m
//  lrlzapp
//
//  Created by dl on 16/2/23.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLMainViewModel.h"
#import "LLMenuItem.h"
#import "LLMenuItemData.h"

#import "LLBannerViewModel.h"
#import "LLBottomTitleViewModel.h"

#define MODULE_TYPE_BANNER                  @"homePageBanner"
#define MODULE_TYPE_QUICK_ENTRY             @"homePageQuickEntry"
#define MODULE_TYPE_HOT_ACTIVITY            @"homePageHotActivity"
#define MODULE_TYPE_ACTIVITY_TILED          @"homePageActivityTiled"

#define MODULE_TYPE_LIST                    @[MODULE_TYPE_BANNER, MODULE_TYPE_QUICK_ENTRY, MODULE_TYPE_HOT_ACTIVITY, MODULE_TYPE_ACTIVITY_TILED]


@interface LLMainViewModel ()
@property(nonatomic, strong, readwrite) NSMutableArray *dataSource;

@property(nonatomic, strong, readwrite) NSArray *bannerDataSource;
@property(nonatomic, strong, readwrite) NSArray *quickEntryDataSource;
@property(nonatomic, strong, readwrite) NSArray *hotActivityDataSource;
@property(nonatomic, strong, readwrite) NSArray *activityTiledDataSource;

@property(nonatomic, strong, readwrite) RACCommand *loadMenusCommand;
@end

@implementation LLMainViewModel

- (void)initialize {
    self.segmentedTitle = @"推荐";
    @weakify(self)

    self.bannerDataSource = @[];
    self.quickEntryDataSource = @[];
    self.hotActivityDataSource = @[];
    self.activityTiledDataSource = @[];

    self.loadMenusCommand =
            [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
                @strongify(self)
                self.isLoading = YES;

                void (^nextBlock)(NSString *, LLMenuItemDataBag *, LLMenuItem *)
                        = ^(NSString *moduleType, LLMenuItemDataBag *bag, LLMenuItem *item) {
                            if ([moduleType isEqualToString:MODULE_TYPE_BANNER]) {
                                LLBannerViewModel *viewModel = [[LLBannerViewModel alloc] init];
                                viewModel.imgs =
                                        [[bag.menuItemDatas.rac_sequence.signal
                                                map:^id(LLMenuItemData *menuItemData) {
                                                    return menuItemData.imgUrl;
                                                }] toArray];
                                viewModel.menuItemDataBag = bag;
                                self.bannerDataSource = @[viewModel];
                            } else {
                                NSArray *array =
                                        [[bag.menuItemDatas.rac_sequence.signal
                                                map:^id(LLMenuItemData *menuItemData) {
                                                    LLBottomTitleViewModel *viewModel = [[LLBottomTitleViewModel alloc] init];
                                                    viewModel.title = menuItemData.name;
                                                    viewModel.imageUrl = menuItemData.imgUrl;
                                                    viewModel.imageSize = IMAGE_SIZE_HALF_SCREEN;
                                                    return viewModel;
                                                }] toArray];
                                if ([moduleType isEqualToString:MODULE_TYPE_QUICK_ENTRY]) {
                                    self.quickEntryDataSource = array;
                                } else if ([moduleType isEqualToString:MODULE_TYPE_HOT_ACTIVITY]) {
                                    self.hotActivityDataSource = array;
                                } else {
                                    [array enumerateObjectsUsingBlock:^(LLBottomTitleViewModel *vm, NSUInteger idx, BOOL *_Nonnull stop) {
                                        vm.imageSize = IMAGE_SIZE_FULL_SCREEN;
                                    }];
                                    self.tiledActivityTitle = item.name;
                                    self.activityTiledDataSource = array;
                                }
                            }
                        };

                NSDictionary *params = @{@"type" : @"homePageTabMain"};
                return [[[[[[LLHTTPSessionManager shareManager]
                        request:URL_CHANNEL_AC_LIST params:params resultClass:LLMenuItemBag.class]
                        flattenMap:^RACStream *(LLMenuItemBag *bag) {
                            return bag.menuItems.rac_sequence.signal;
                        }]
                        flattenMap:^RACStream *(LLMenuItem *item) {
                            if ([MODULE_TYPE_LIST containsObject:item.identifier]) {
                                NSDictionary *params = @{@"type" : item.identifier};
                                RACSignal *signal = [[LLHTTPSessionManager shareManager]
                                        request:URL_CHANNEL_AC_ACTIVITY_LIST
                                         params:params
                                    resultClass:[LLMenuItemDataBag class]];
                                return [RACSignal combineLatest:@[
                                        [RACSignal return:item.identifier],
                                        signal,
                                        [RACSignal return:item]
                                ]];
                            }
                            return [RACSignal empty];
                        }]
                        doNext:^(RACTuple *tupe) {
                            nextBlock(tupe.first, tupe.second, tupe.third);
                        }]
                        finally:^{
                            // 第四个，显示Title，并且增加下面的阴影
                            [(NSArray *) self.activityTiledDataSource enumerateObjectsUsingBlock:^(LLBottomTitleViewModel *vm, NSUInteger idx, BOOL *_Nonnull stop) {
                                vm.showTitle = YES;
                                vm.showGradient = YES;
                            }];
                            self.dataSource = @[self.bannerDataSource,
                                    self.quickEntryDataSource,
                                    self.hotActivityDataSource,
                                    self.activityTiledDataSource
                            ];
                            self.isLoading = NO;
                        }];
            }];

    [self.loadMenusCommand.errors
            subscribeNext:^(id x) {
                self.isLoading = NO;
            }];
}

@end
